#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 二八十六进制整数
问题: 你需要转换或者输出使用二进制，八进制或十六进制表示的整数。
提示 : 
"""

def recipe1():
    """整数转换"""
    x = 1234
    print(f"{x = }")
    print(f"{bin(x) = }")       # 二进制
    print(f"{oct(x) = }")       # 八进制
    print(f"{hex(x) = }")       # 16进制

    print(f"{format(x, 'b') = }")
    print(f"{format(x, 'o') = }")
    print(f"{format(x, 'x') = }")

def recipe2():
    """负数转换"""
    x = -1234
    print(f"{x = }")
    print(f"{format(x, 'b') = }")
    print(f"{format(x, 'o') = }")
    print(f"{format(x, 'x') = }")

def recipe3():
    """产生一个无符号值"""
    x = -1234
    print(f"{x = }")
    print(f"{format(2**32 + x, 'b') = }")
    print(f"{format(2**32 + x, 'o') = }")
    print(f"{format(2**32 + x, 'x') = }")

def recipe4():
    """以不同的进制转换整数字符串"""
    print(f"{int('4d2', 16) = }")
    print(f"{int('10011010010', 2) = }")

def recipe5():
    """Python指定八进制数的语法跟其他语言稍有不同"""
    import os
    #os.chmod('script.py', 0755)
    os.chmod('04_bin_octal.py', 0o755)

def main():
    print('recipe1'.center(20, '*'))
    recipe1() 
    print('recipe2'.center(20, '*'))
    recipe2() 
    print('recipe3'.center(20, '*'))
    recipe3() 
    print('recipe4'.center(20, '*'))
    recipe4() 
    print('recipe5'.center(20, '*'))
    recipe5() 

if __name__ == '__main__':
    main()       